package com.member.web.controller.shop;

import com.member.shop.domain.SysBizGoods;
import com.member.shop.service.ISysBizUserService;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.domain.model.LoginUser;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizGradeInfo;
import com.member.shop.service.ISysBizGradeInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 用户等级Controller
 *
 * @author thomas
 * @date 2024-06-28
 */
@RestController
@RequestMapping("/shop/other/grade")
public class SysBizGradeInfoController extends BaseController {
    @Resource
    private ISysBizGradeInfoService sysBizGradeInfoService;

    private void setTenantId(SysBizGradeInfo sysBizGradeInfo) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizGradeInfo.setTenantId(tenantId);
        }
    }

    /**
     * 查询用户等级列表
     */
    @PreAuthorize("@ss.hasPermi('other:grade:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizGradeInfo sysBizGradeInfo) {
        startPage();
        setTenantId(sysBizGradeInfo);
        List<SysBizGradeInfo> list = sysBizGradeInfoService.selectSysBizGradeInfoList(sysBizGradeInfo);
        return getDataTable(list);
    }

    /**
     * 导出用户等级列表
     */
    @PreAuthorize("@ss.hasPermi('other:grade:export')")
    @Log(title = "用户等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizGradeInfo sysBizGradeInfo) {
        setTenantId(sysBizGradeInfo);
        List<SysBizGradeInfo> list = sysBizGradeInfoService.selectSysBizGradeInfoList(sysBizGradeInfo);
        ExcelUtil<SysBizGradeInfo> util = new ExcelUtil<SysBizGradeInfo>(SysBizGradeInfo.class);
        util.exportExcel(response, list, "用户等级数据");
    }

    /**
     * 获取用户等级详细信息
     */
    @PreAuthorize("@ss.hasPermi('other:grade:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysBizGradeInfoService.selectSysBizGradeInfoById(id));
    }

    /**
     * 新增用户等级
     */
    @PreAuthorize("@ss.hasPermi('other:grade:add')")
    @Log(title = "用户等级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizGradeInfo sysBizGradeInfo) {

        LoginUser loginUser = this.getLoginUser();

        String result = checkSysBizGradeInfoByGradeNo(sysBizGradeInfo);

        if (StringUtils.isNotEmpty(result)) {
            return AjaxResult.error(result);
        }
        sysBizGradeInfo.setCreateBy(loginUser.getUsername());
        sysBizGradeInfo.setUpdateBy(loginUser.getUsername());
        sysBizGradeInfo.setUpdateTime(new Date());

        sysBizGradeInfo.setCreateTime(new Date());
        return toAjax(sysBizGradeInfoService.insertSysBizGradeInfo(sysBizGradeInfo));
    }

    private String checkSysBizGradeInfoByGradeNo(SysBizGradeInfo sysBizGradeInfo) {
        SysBizGradeInfo bizGradeInfo = sysBizGradeInfoService.selectSysBizGradeInfoByGrade(sysBizGradeInfo.getGradeNo());
        String result = "";
        if (bizGradeInfo != null) {
            if (sysBizGradeInfo.getId() == null || sysBizGradeInfo.getId() != bizGradeInfo.getId()) {
                result = "等级编号必须唯一";

            }

        }


        return result;

    }

    /**
     * 修改用户等级
     */
    @PreAuthorize("@ss.hasPermi('other:grade:edit')")
    @Log(title = "用户等级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizGradeInfo sysBizGradeInfo) {

        LoginUser loginUser = this.getLoginUser();
        sysBizGradeInfo.setUpdateBy(loginUser.getUsername());
        sysBizGradeInfo.setUpdateTime(new Date());

        String result = checkSysBizGradeInfoByGradeNo(sysBizGradeInfo);

        if (StringUtils.isNotEmpty(result)) {
            return AjaxResult.error(result);
        }

        return toAjax(sysBizGradeInfoService.updateSysBizGradeInfo(sysBizGradeInfo));
    }

    /**
     * 删除用户等级
     */
    @PreAuthorize("@ss.hasPermi('other:grade:remove')")
    @Log(title = "用户等级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizGradeInfoService.deleteSysBizGradeInfoByIds(ids));
    }

    @Autowired
    private ISysBizUserService bizUserService;


//    /**
//     * 删除用户等级
//     */
//    @PreAuthorize("@ss.hasPermi('other:grade:remove')")
//    @Log(title = "用户等级", businessType = BusinessType.DELETE)
//    @PostMapping("/delete/{grade}")
//    public AjaxResult remove(@PathVariable("grade") Integer grade) {
//        List<SysBizUser> sysBizUsers = bizUserService.getSysBizUserByGrade(grade);
//
//        if (!CollectionUtils.isEmpty(sysBizUsers)) {
//            return AjaxResult.error("当前等级存在用户无法删除");
//        }
//        List<SysBizMedal> sysBizMedalListByTypeAndGrade = bizMedalService.getSysBizMedalListByTypeAndGrade(0, grade);
//
//        if (!CollectionUtils.isEmpty(sysBizMedalListByTypeAndGrade)) {
//            return AjaxResult.error("当前等级存在等级勋章无法删除");
//        }
//
//
//        return toAjax(sysBizGradeInfoService.deleteSysBizGradeInfoByGrade(grade));
//    }
}
